import subprocess
import sys

def run_git_command(command):
    try:
        # 使用 subprocess.run() 执行命令并捕获输出
        result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"Error executing command: {command}")
        print(e.stderr)
        sys.exit(1)

def git_add():
    command = ["git", "add", "."]
    run_git_command(command)

def git_commit(message):
    command = ["git", "commit", "-m", message]
    run_git_command(command)

def git_push():
    command = ["git", "push"]
    run_git_command(command)

if __name__ == "__main__":
    commit_message = input("Enter your commit message: ")
    
    # 添加所有文件到暂存区
    git_add()
    
    # 提交更改
    git_commit(commit_message)
    
    # 推送到远程仓库
    git_push()
    
    print("Code has been successfully pushed to the remote repository.")